package com.ysd.lis.util.sql;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class EntityUtils {
    public static void setProperty(Object obj, String propertyName, Object value) throws NoSuchFieldException, IllegalAccessException {
        try {
            if (value == null) {
                return;
            }
            Field field = obj.getClass().getDeclaredField(propertyName);

            if (field == null) {
                return;
            }
            field.setAccessible(true);
            Class<?> fieldType = field.getType();
            // 根据字段的类型进行赋值
            if (fieldType == String.class) {
                field.set(obj, value.toString());
            } else if (fieldType == int.class || fieldType == Integer.class) {
                if (value.toString().equals("0")) {
                    field.set(obj, 0);
                } else if (value.toString().equals("1")) {
                    field.set(obj, 1);
                } else {
                    field.set(obj, (Integer) value);
                }
            } else if (fieldType == boolean.class || fieldType == Boolean.class) {
                field.set(obj, (Boolean) value);
            } else if (fieldType == Timestamp.class) {
                /*需要区分不同数据库*/
                if (false) {
                    field.set(obj, value.toString());//sqlservewr
                } else {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 使用 SimpleDateFormat 对象解析字符串并得到 java.util.Date 对象
                    java.util.Date parsedDate = dateFormat.parse(value + "");

                    // 将 java.util.Date 对象转换为 java.sql.Timestamp 对象
                    java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
                    field.set(obj, timestamp);
                    //field.set(obj, (Timestamp) value);
                }
            } else if (fieldType == java.sql.Timestamp.class) {
                try {
                    if (false) {
                        field.set(obj, value.toString());//sqlservewr
                    } else {
                        //java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf((String) value);
                        // 创建 SimpleDateFormat 对象来解析日期时间字符串
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                        // 使用 SimpleDateFormat 对象解析字符串并得到 java.util.Date 对象
                        java.util.Date parsedDate = dateFormat.parse(value + "");

                        // 将 java.util.Date 对象转换为 java.sql.Timestamp 对象
                        java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
                        field.set(obj, timestamp);
                    }
                } catch (ParseException e) {
                    //TODO
                }
            } else if (fieldType == BigDecimal.class) {
                BigDecimal bigDecimalNumber = convertToBigDecimal(value);
                // 保留两位小数，使用四舍五入模式
                BigDecimal roundedNumber = bigDecimalNumber.setScale(2, RoundingMode.HALF_UP);
                field.set(obj, roundedNumber);
            } else {
                field.set(obj, value);
            }
        } catch (Exception e) {
            //ToDo
        }
    }

    public static BigDecimal convertToBigDecimal(Object number) {
        if (number == null) {
            return null;
        }
        if (number instanceof BigDecimal) {
            return (BigDecimal) number;
        } else if (number instanceof String) {
            return new BigDecimal(number.toString().trim());
        } else if (number instanceof Integer) {
            return new BigDecimal(number.toString().trim());
        } else if (number instanceof Number) {
            return new BigDecimal(number.toString().trim());
        } else {
            return null;
        }
    }
}
